package chess.server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;

import chess.model.Session;

public class ServerConnection extends Thread {

	private boolean connected = false;

	private boolean authenticated;

	private Socket fics;

	private BufferedReader br;

	private BufferedWriter bw;

	private int letter;

	private String line = "";

	private String server;

	private int port;

	private ArrayList<NewLineListener> listeners = new ArrayList<NewLineListener>();

	public ServerConnection(String server, int port) {
		System.out.println("pripojovani");
		this.server = server;
		this.port = port;
	}

	public void connect() throws SocketException {
		try {
			fics = new Socket(server, port);
			bw = new BufferedWriter(new OutputStreamWriter(fics
					.getOutputStream()));
			br = new BufferedReader(
					new InputStreamReader(fics.getInputStream()));
			System.out.println("pripojeno");
			connected = true;
			Session session = Session.getInstance();
			session.setConnection(this);
			this.start();
		} catch (UnknownHostException e) {
			connected = false;
			// e.printStackTrace();
			throw new SocketException();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void run() {
		while (true) {
			try {
				line = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.out.println(line);
			if (line == null) {
				this.interrupt();
				try {
					// wait till this thread dies
					this.join();
				} catch (InterruptedException e) {
					// safe to ignore this error
				}
				break;
			}
			if (!line.equals("")) {
				dispatchEvent(line);
			}
			line = "";
		}
	}

	public int readLetter() {
		try {
			letter = br.read();
			System.out.write(letter);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return letter;
	}

	public void sendMessage(String text) {
		try {
			bw.write(text + "\n");
			bw.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public synchronized void login(String login, String password) {
		sendMessage(login);
		sendMessage(password);
		authenticated = true;
	}

	public void addPacketWriterListener() {

	}

	public void removePacketWriterListener() {

	}

	public boolean isConnected() {
		return connected;
	}

	public void disconnect() {

	}

	public boolean isAuthenticated() {
		return authenticated;
	}

	public void setAuthenticated(boolean auth) {
		this.authenticated = auth;
	}

	// register new listener
	public void addNewLineListener(NewLineListener newLineListener) {
		listeners.add(newLineListener);
	}

	// removes the listener
	public void removeNewLineLIstener(NewLineListener newLineListener) {
		listeners.remove(newLineListener);
	}

	void dispatchEvent(String line) {
		for (int i = 0; i < listeners.size(); i++) {
			NewLineListener newLineListener = (NewLineListener) listeners
					.get(i);
			if (newLineListener != null) {
				NewLineEvent newLineEvent = new NewLineEvent(this, line);
				newLineListener.handleNewLineEvent(newLineEvent);
			}
		}
	}
}
